package com.xxx.shetuan3.controller;

import com.xxx.shetuan3.common.CommonResult;
import com.xxx.shetuan3.dto.NoticeDTO;
import com.xxx.shetuan3.exception.ApiException;
import com.xxx.shetuan3.pojo.Notice;
import com.xxx.shetuan3.service.NoticeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @ApiOperation("根据notice id查询公告")
    @GetMapping("/{noticeId}")
    public CommonResult<Notice> queryNoticeById(@NotNull @PathVariable(value = "noticeId") Integer noticeId){
        Notice notice = noticeService.queryNoticeById(noticeId);
        if(notice==null){
            throw new ApiException("未找到notice");
        }else{
            return CommonResult.success(notice);
        }
    }

    @ApiOperation("根据noticeId 删除公告")
    @DeleteMapping("/{noticeId}")
    public CommonResult deleteNoticeById(@NotNull @PathVariable(value = "noticeId") Integer noticeId){
        Notice notice = noticeService.queryNoticeById(noticeId);
        if(notice==null){
            throw new ApiException("notcie does't exist");
        }
        boolean res = noticeService.deleteNoticeById(noticeId);
        if(res){
            return CommonResult.success("删除成功");
        }else{
            throw new ApiException("删除失败");
        }

    }

    @ApiOperation("更新notice")
    @PostMapping("/{noticeId}")
    public CommonResult updateNotice(@PathVariable(value = "noticeId") Integer noticeId, @Valid NoticeDTO noticeDTO){
        Notice notice = new Notice(noticeDTO);
        notice.setNoticeId(noticeId);
        boolean res = noticeService.updateNotice(notice);
        if(res){
            return CommonResult.success("更新成功");
        }else{
            throw new ApiException("更新失败");
        }
    }

    @ApiOperation("添加一个公告")
    @PostMapping("/")
    public CommonResult saveNotice(@Valid NoticeDTO noticeDTO){
        Notice notice = new Notice(noticeDTO);
        notice.setDate(new Date(System.currentTimeMillis()));
        boolean res = noticeService.saveNotice(notice);
        if(res){
            return CommonResult.success("添加成功");
        }else{
            throw new ApiException("添加失败");
        }
    }

    @ApiOperation("分页查询")
    @GetMapping("/list")
    public CommonResult<List<Notice>> pageNotice(@RequestParam(value = "start",defaultValue = "1") Integer start,
                                                 @RequestParam(value = "size",defaultValue = "10") Integer size,
                                                 Integer organizationId){
        List<Notice> noticeList = noticeService.queryNoticePageByOrganizationId(start, size, organizationId);
        return CommonResult.success(noticeList);
    }
}
